﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using TakaGUI.Cores;
using TakaGUI.DrawBoxes.Forms;
using TakaGUI.DrawBoxes;
using EvoSim.DrawBoxes;
using EvoSim.Simulation;
using TakaGUI.Services;
using EvoSim.Simulation.Genes;
using EvoSim.Simulation.Creatures;
using TakaGUI;
using EvoSim.Simulation.NeuralNets.SpikeNet;
using EvoSim.Simulation.NeuralNets.RMP;
using EvoSim.Simulation.NeuralNets;

namespace EvoSim.States
{
	public class InitializeState : State
	{
		WorldBox worldBox;

		public override void Initialize(GameTime gameTime)
		{
			Globals.GameScreen = new TakaGUI.Window();
			Globals.GUIManager.GameScreens.Add(Globals.GameScreen);

			Globals.KeyboardInput.Enabled = true;
			Globals.MouseInput.Enabled = true;
			Globals.GUIManager.Enabled = true;
			Globals.GUIManager.Visible = true;

			FpsMeter fpsMeter = new FpsMeter();
			fpsMeter.Initialize();
			Globals.GameScreen.AddDrawBox(fpsMeter);

			worldBox = new WorldBox();
			worldBox.Initialize();
			Globals.GameScreen.AddDrawBox(worldBox);
			worldBox.FillWidth();
			worldBox.FillHeight();
			worldBox.ViewCamera.LookX = 0;
			worldBox.ViewCamera.LookY = 0;

			Form form = new Form();
			form.Initialize();
			Globals.GameScreen.AddDrawBox(form);
			form.Width = 300;
			form.Height = 300;

			Form form2 = new Form();
			form2.Initialize();
			Globals.GameScreen.AddDrawBox(form2);
			form2.Width = 300;
			form2.Height = 300;
			Push.ToTheBottomSideOf(form2, form, 10, Push.VerticalAlign.Left);

			EvoSim.Simulation.Worlds.RandomFoodWorld world = new EvoSim.Simulation.Worlds.RandomFoodWorld();
			world.Width = worldBox.Width;
			world.Height = worldBox.Height;

			//var controller = new RMPController();
			//controller.CreatureGenome = new HostGenome();
			//controller.CreatureGenome.SetNetChromosome(new RMP_Chromosome());
			//controller.Randomize();
			//world.AddEntity(controller);
			//controller.PosX = world.Width / 2;
			//controller.PosY = world.Height / 2;

			var cviz = new CreatureVisionBox();
			cviz.Initialize(null);
			form.AddDrawBox(cviz);
			cviz.Fill();
			cviz.Alignment = DrawBoxAlignment.GetFull();
			cviz.SelectingPool = worldBox;

			var gSettings = new GenomeSettings();
			gSettings.Crossover = new SinglePointCrossover();
			var nSettings = new RMP_ChromsomeSettings();
			var sigmoid = new DefaultSigmoid(true);
			foreach (var gene in sigmoid.GeneList)
				gene.IsMutable = false;

			for (int i = 0; i < 30; i++)
			{
				var creature = new SimpleCreature();
				creature.CreatureGenome = new Genome(gSettings);
				var netChromosome = new RMP_Chromosome(nSettings);
				netChromosome.SettSigmoidFunction(sigmoid);
				creature.CreatureGenome.SetNetChromosome(netChromosome);

				creature.Randomize();
				world.AddEntity(creature);

				creature.PosX = Globals.Random.Next(world.Width);
				creature.PosY = Globals.Random.Next(world.Height);
				creature.Rotation = Math.PI * 2 * Globals.Random.NextDouble();
			}

			var viz = new RMP_NetNumberVisualizer();
			viz.Initialize();
			form2.AddDrawBox(viz);
			viz.FillWidth();
			viz.FillHeight();
			viz.Alignment = TakaGUI.DrawBoxAlignment.GetFull();
			viz.Container.PutChildInBack(viz);
			viz.SelectingPool = worldBox;

			worldBox.World = world;
		}

		public override void Work(GameTime gameTime)
		{
		}
	}
}
